package services

import (
	"HyperNexus/common/types"
	"HyperNexus/hyperNexus/interfaces"
	"github.com/gogf/gf/v2/container/gmap"
)

type ServiceMaster struct {
	allServices *gmap.StrAnyMap
}

func NewServiceMaster() *ServiceMaster {
	sm := &ServiceMaster{
		allServices: gmap.NewStrAnyMap(true),
	}
	return sm
}

func (sm *ServiceMaster) AddService(is interfaces.IService) error {
	sm.allServices.Set(string(is.Sid()), is)
	if err := is.Init(); err != nil {
		return err
	}
	return nil
}

func (sm *ServiceMaster) StartService(sid types.ServiceUUID) error {
	is := sm.allServices.Get(string(sid)).(interfaces.IService)
	return is.Start()
}

func (sm *ServiceMaster) RunService(sid types.ServiceUUID) error {
	is := sm.allServices.Get(string(sid)).(interfaces.IService)
	err := is.Run()
	return err
}

func (sm *ServiceMaster) StopService(sid types.ServiceUUID) error {
	is := sm.allServices.Get(string(sid)).(interfaces.IService)
	err := is.Stop()
	return err
}
